package com.denlaku.longan.repository;

import com.denlaku.longan.anno.VarOp;
import com.denlaku.longan.core.Current;
import com.denlaku.longan.dao.DataTagRelDao;
import com.denlaku.longan.qo.DataPermQuery;
import com.denlaku.longan.vo.DataTagRel;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author tianx
 */
@Repository
public class DataTagRelRepository {
    @Autowired
    private DataTagRelDao dataTagRelDao;

    /**
     * 批量绑定数据标签
     *
     * @param relList 绑定标签列表
     */
    @VarOp
    public void batchAdd(List<DataTagRel> relList) {
        Long tenantId = Current.getTenantId();
        Map<Long, List<DataTagRel>> map = ListUtils.emptyIfNull(relList).stream()
            .collect(Collectors.groupingBy(DataTagRel::getTagPackId));
        map.forEach((tagsId, list) -> dataTagRelDao.batchAdd(tenantId, tagsId, list));
    }

    /**
     * 查询绑定数据标签列表
     *
     * @param query 查询参数
     * @return 绑定标签列表
     */
    public List<DataTagRel> list(DataPermQuery query) {
        return dataTagRelDao.list(query);
    }

    /**
     * 删除绑定数据标签
     *
     * @param query 查询参数
     */
    public void delete(DataPermQuery query) {
        dataTagRelDao.delete(query);
    }
}
